<template>
  <div class="business-list-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">商家列表</div>
    </div>

    <!-- 商家列表 -->
    <div class="business-list">
      <div class="business-item"
           v-for="item in businessList"
           :key="item.businessId"
           @click="goToBusinessInfo(item.businessId)">
        <div class="business-img">
          <img :src="item.businessImg" :alt="item.businessName">
          <!-- 如果有购物车数量则显示 -->
          <span class="cart-count" v-if="item.cartCount">{{item.cartCount}}</span>
        </div>
        <div class="business-info">
          <h3>{{item.businessName}}</h3>
          <div class="business-desc">{{item.businessExplain}}</div>
          <div class="business-data">
            <div class="rating">
              <span class="score">4.9</span>
              <span class="sales">月售345单</span>
            </div>
            <div class="delivery">
              <span>{{item.deliveryPrice}}元配送</span>
              <span>{{item.starPrice}}元起送</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const businessList = ref([])
const userId = localStorage.getItem('userId') // 获取登录用户ID

// 获取商家列表
const getBusinessList = async () => {
  try {
    const typeId = route.query.typeId
    const res = await axios.get('http://localhost:8080/elm/business/listBusinessByOrderTypeId', {
      params: { orderTypeId: typeId }
    })
    businessList.value = res.data

    // 如果用户已登录，查询购物车信息
    if (userId) {
      await getCartInfo()
    }
  } catch (error) {
    console.error('获取商家列表失败:', error)
  }
}

// 获取购物车信息
const getCartInfo = async () => {
  try {
    const res = await axios.get('http://localhost:8080/elm/cart/listCart', {
      params: { userId }
    })
    // 将购物车数量添加到对应的商家信息中
    const cartInfo = res.data
    businessList.value = businessList.value.map(business => {
      const businessCart = cartInfo.filter(cart => cart.businessId === business.businessId)
      const cartCount = businessCart.reduce((total, cart) => total + cart.quantity, 0)
      return {
        ...business,
        cartCount: cartCount || 0
      }
    })
  } catch (error) {
    console.error('获取购物车信息失败:', error)
  }
}

// 跳转到商家详情
const goToBusinessInfo = (businessId) => {
  router.push({
    path: '/businessInfo',
    query: { businessId }
  })
}

onMounted(() => {
  getBusinessList()
})
</script>

<style scoped>
.business-list-page {
  padding-top: 44px;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 16px;
}

.business-list {
  background: #fff;
}

.business-item {
  display: flex;
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.business-img {
  position: relative;
  width: 80px;
  height: 80px;
  margin-right: 10px;
}

.business-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cart-count {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff2d4b;
  color: #fff;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

.business-info {
  flex: 1;
}

.business-info h3 {
  font-size: 16px;
  margin: 0 0 5px;
}

.business-desc {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.business-data {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}

.rating .score {
  color: #ff6b01;
  margin-right: 5px;
}

.rating .sales {
  color: #666;
}

.delivery {
  color: #666;
}

.delivery span {
  margin-left: 10px;
}
</style>